'use strict';

app.controller('checkOutCtrl', function ($rootScope, $scope, $http) {

    // 购物车列表
    $scope.checkshopData = $rootScope.shopList;
    
    // 订单数组
    $scope.food_form = [];

    //计算总价 
    $scope.allSum = function(){  
        var allPrice = 0;  
        for(var i= 0;i<$scope.checkshopData.length;i++){  
            allPrice+=$scope.checkshopData[i].price*$scope.checkshopData[i].number;  
            // console.log(allPrice);
        }  
        return allPrice;  
    };

    // 计算找零
    $scope.getChange = function(){

        var allPrice = $scope.allSum();
        var getChange = parseInt($scope.getMoney) - parseInt(allPrice);
        if (getChange >= 0) {
            return getChange;
        }
        
    }

    //减少  
    $scope.reduce = function (index) {
        // console.log("-----");
        if($scope.checkshopData[index].number>1){  
            $scope.checkshopData[index].number--;  
        }else{  
            // $scope.remove(index);  
        }  
    };  
    //增加  
    $scope.add = function(index){ 
        // console.log("++++"); 
        $scope.checkshopData[index].number++;  
    };

    // 生成时间流水号
    $scope.timeTostring = function(){
        var now = new Date();
        var year = now.getFullYear();
        var month =(now.getMonth() + 1).toString();
        var day = (now.getDate()).toString();
        var hour = (now.getHours()).toString();
        var minute = (now.getMinutes()).toString();
        var second = (now.getSeconds()).toString();
        if (month.length == 1) {
            month = "0" + month;
        }
        if (day.length == 1) {
            day = "0" + day;
        }
        var dateTime = year + month +  day + hour + minute + second;
        return dateTime;
    };

    // 生成日期
    $scope.creatDate = function(){
        var now = new Date();
        var year = now.getFullYear();
        var month =(now.getMonth() + 1).toString();
        var day = (now.getDate()).toString();
        var hour = (now.getHours()).toString();
        var minute = (now.getMinutes()).toString();
        var second = (now.getSeconds()).toString();
        if (month.length == 1) {
            month = "0" + month;
        }
        if (day.length == 1) {
            day = "0" + day;
        }
        var dateTime = year + '-' + month + '-' +  day + ' ' + hour + ':' + minute + ':' + second;
        return dateTime;
    };



    // 生成订单
    $scope.generateOrder = function(obj){
        var order = $scope.timeTostring();//生成订单号
        var form = {
            "order_number": order,
            "number": Math.round(Math.random()*1000).toString(),
            "money": $scope.allSum(),
            "time": $scope.creatDate(),
            "user_id": "",
            "remarks": document.getElementById("foodremark").value,
            "food_detail": obj
        };
        // form.order_number = order;
        // form.number = Math.round(Math.random()*1000).toString();
        // form.money = $scope.allSum();
        // form.time = $scope.creatDate();
        // form.user_id = "";
        // form.remarks = document.getElementById("foodremark").value;
        // form.food_detail = obj;
        return form;
    };


    // 控制结算显示与隐藏
    $scope.food_check = true;
    $scope.foodCheckout = function(){
        $scope.food_check = !$scope.food_check;
    };


    // 打印
    var LODOP; //声明为全局变量
    // 预览
    $scope.printPreview = function(data){  
        //创建小票打印页  
        LODOP = $scope.CreatePrintPage(data);  
        //打印预览  
        LODOP.PREVIEW();      
    };
    // 打印
    $scope.printOrder = function(data) {  
          
        //创建小票打印页  
        LODOP = $scope.CreatePrintPage(data);  
        //开始打印  
        LODOP.PRINT(); 
        //打印预览  
        //LODOP.PREVIEW();   
      
    };
    //创建小票打印页
    $scope.CreatePrintPage =function(data) {
        var json = data;
        console.log(json); 
        // 当前时间
        var Time;
        var today = new Date();
        Time = today.toLocaleString();

        var hPos=10,//小票上边距  
        pageWidth=580,//小票宽度  
        rowHeight=15,//小票行距  
        //获取控件对象  
        LODOP=getLodop(document.getElementById('LODOP_OB'),document.getElementById('LODOP_EM'));   
        //初始化   
        // LODOP.PRINT_INIT("打印控件功能演示_Lodop功能_名片"); 
        LODOP.PRINT_INIT(""); 
        //添加小票标题文本  
        LODOP.ADD_PRINT_TEXT(hPos,30,pageWidth,rowHeight,$rootScope.shop_set.name);  
        //上边距往下移  
        hPos+=rowHeight;  
          
        LODOP.ADD_PRINT_TEXT(hPos,1,pageWidth,rowHeight,"订单编号:");  
        LODOP.ADD_PRINT_TEXT(hPos,60,pageWidth,rowHeight,json.order_number);  
        hPos+=rowHeight; //换行  
        LODOP.ADD_PRINT_TEXT(hPos,1,pageWidth,rowHeight,"下单时间:");  
        LODOP.ADD_PRINT_TEXT(hPos,60,pageWidth,rowHeight,Time);  
        hPos+=rowHeight;  
        LODOP.ADD_PRINT_TEXT(hPos,1,pageWidth,rowHeight,"餐号:");  
        LODOP.ADD_PRINT_TEXT(hPos,60,pageWidth,rowHeight,json.number);  
        hPos+=rowHeight;  
        LODOP.ADD_PRINT_TEXT(hPos,1,pageWidth,rowHeight,"备注:");  
        LODOP.ADD_PRINT_TEXT(hPos,40,pageWidth,rowHeight,json.remarks);  
        hPos+=rowHeight;  
        // LODOP.ADD_PRINT_TEXT(hPos,1,pageWidth,rowHeight,"取货门店:");  
        // LODOP.ADD_PRINT_TEXT(hPos,60,pageWidth,rowHeight,json.shop);  
        // hPos+=rowHeight;  
        LODOP.ADD_PRINT_LINE(hPos,2, hPos, pageWidth,2, 1);  
        hPos+=5;  
        LODOP.ADD_PRINT_TEXT(hPos,1,pageWidth,rowHeight,"商品名称");  
        LODOP.ADD_PRINT_TEXT(hPos,70,pageWidth,rowHeight,"单价");  
        LODOP.ADD_PRINT_TEXT(hPos,110,pageWidth,rowHeight,"数量");  
        LODOP.ADD_PRINT_TEXT(hPos,140,pageWidth,rowHeight,"小计");  
        hPos+=rowHeight;  
        //遍历json的商品数组  
        for(var i=0;i<json.food_detail.length;i++){  
              
            if(json.food_detail[i].name.length<4){  
                LODOP.ADD_PRINT_TEXT(hPos,1,pageWidth,rowHeight,json.food_detail[i].name);  
            }else {  
                //商品名字过长,其他字段需要换行  
                LODOP.ADD_PRINT_TEXT(hPos,1,pageWidth,rowHeight,json.food_detail[i].name);  
                hPos+=rowHeight;  
            }  
            LODOP.ADD_PRINT_TEXT(hPos,70,pageWidth,rowHeight,json.food_detail[i].price);  
            LODOP.ADD_PRINT_TEXT(hPos,115,pageWidth,rowHeight,json.food_detail[i].number);  
            LODOP.ADD_PRINT_TEXT(hPos,140,pageWidth,rowHeight,json.food_detail[i].price*json.food_detail[i].number);  
            hPos+=rowHeight;  
        }  
        //商品遍历打印完毕,空一行
        LODOP.ADD_PRINT_LINE(hPos,2, hPos, pageWidth,2, 1);  
        hPos+=rowHeight;  
        //合计  
        // LODOP.ADD_PRINT_TEXT(hPos,80,pageWidth,rowHeight,"合计:"+json.totalCount);
        LODOP.ADD_PRINT_TEXT(hPos,80,pageWidth,rowHeight,"合计:");  
        LODOP.ADD_PRINT_TEXT(hPos,130,pageWidth,rowHeight,"￥"+json.money);  
          
        hPos+=rowHeight;  
        // LODOP.ADD_PRINT_TEXT(hPos,2,pageWidth,rowHeight,(new Date()).toLocaleDateString()+" "+(new Date()).toLocaleTimeString())  
        hPos+=rowHeight;  
        LODOP.ADD_PRINT_TEXT(hPos,25,pageWidth,rowHeight,"谢谢惠顾,欢迎下次光临!");  
        //初始化打印页的规格  
        LODOP.SET_PRINT_PAGESIZE(3,pageWidth,45,"XXXXX订单信息");  
        return LODOP; 
    };



    // 现金支付
    $scope.cashPayment = function(){
        $scope.food_form = $scope.generateOrder($scope.checkshopData);
        console.log($scope.food_form);

        // 向后台发送数据
        // $http.get('/api/generateOrders',{params:{"data":$scope.food_form}})
        $http.post('/api/generateOrders',$scope.food_form)
        .then(function(response){
            if(response.status==200){
                console.log(response);

                if(confirm('订单已完成！')){ 
                    // 打印小票
                    $scope.printOrder($scope.food_form);  
                    $rootScope.shopList = [];
                    location.href = 'http://'+ location.hostname + ':3000/#/app/orderCash';
                    location.reload();

                }
                
            }
            else{
                console.log(response.status);
            }
        });

        
    };

    // 微信支付
    $scope.wechatPayment = function(){
        // console.log($scope.checkshopData);
        $scope.food_form = $scope.generateOrder($scope.checkshopData);

    };

    // 支付宝支付
    $scope.alipayPayment = function(){
        // console.log($scope.checkshopData);
        $scope.food_form = $scope.generateOrder($scope.checkshopData);

    };

});